﻿Imports System.Globalization
Imports SecretaryToolData
Imports System.Text.RegularExpressions

Public Class frmPublisherData

	Private _PublisherID As Long?

	Private ReadOnly Property TypedPublisherInformationBindingSource As VM_PublisherInformation
		Get
			Return CType(PublisherInformationBindingSource.DataSource, VM_PublisherInformation)
		End Get
	End Property

	Private Sub frmPublisherData_Load(sender As Object, e As System.EventArgs) Handles Me.Load

		PublisherInformationBindingSource.DataSource = New VM_PublisherInformation(_PublisherID)

		'initialize began publishing date so that it saves to the database
		If _PublisherID Is Nothing Then
			TypedPublisherInformationBindingSource.BeganPublishing = Now
		End If

		If CType(PublisherInformationBindingSource.DataSource, VM_PublisherInformation).CeasedPublishing Is Nothing Then
			dateCeasedPublishing.Format = DateTimePickerFormat.Custom
			dateCeasedPublishing.CustomFormat = " "
		End If
		If CType(PublisherInformationBindingSource.DataSource, VM_PublisherInformation).BirthDate Is Nothing Then
			dateBirth.Format = DateTimePickerFormat.Custom
			dateBirth.CustomFormat = " "
		End If
		If CType(PublisherInformationBindingSource.DataSource, VM_PublisherInformation).ImmersionDate Is Nothing Then
			dateImmersion.Format = DateTimePickerFormat.Custom
			dateImmersion.CustomFormat = " "
		End If

		'because the binding value is a string, and we have an integer
		'we have to set this manually
		cmboTextMessageEmailDomain.SelectedValue = TypedPublisherInformationBindingSource.MobileProviderID.ToString

	End Sub

	Public Sub New(Optional PublisherID As Long? = Nothing)

		InitializeComponent()

		_PublisherID = PublisherID

		If PublisherID Is Nothing Then
			btnPrintHeader.Enabled = False
		End If

	End Sub

	Private Sub dateBirth_ValueChanged(sender As Object, e As System.EventArgs) Handles dateBirth.ValueChanged
		dateBirth.Format = DateTimePickerFormat.Long
	End Sub

	Private Sub dateImmersion_ValueChanged(sender As Object, e As System.EventArgs) Handles dateImmersion.ValueChanged
		dateImmersion.Format = DateTimePickerFormat.Long
	End Sub

	Private Sub dateCeasedPublishing_ValueChanged(sender As Object, e As System.EventArgs) Handles dateCeasedPublishing.ValueChanged
		dateCeasedPublishing.Format = DateTimePickerFormat.Long
	End Sub

	Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
		Me.Close()
	End Sub

	Private Sub btnPrintHeader_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintHeader.Click
		Dim reportLocation As String = Reports.PublisherRecordCardHeader(_PublisherID).FullName
		If MessageBox.Show(String.Format("Report generated and saved at{0}{1}.{0}{0}Do you want to open it?", Environment.NewLine, reportLocation), "Report Successfully Generated", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
			Process.Start(reportLocation)
		End If
	End Sub

	Private Sub btnEmergencyContactInformation_Click(sender As System.Object, e As System.EventArgs) Handles btnEmergencyContactInformation.Click
		Dim frmEmergencyContact As New frmEmergencyContact(cmboFamily.SelectedValue)
		frmEmergencyContact.ShowDialog()
		PublisherInformationBindingSource.ResetBindings(False)
	End Sub

	Private Sub btnLookupProvider_Click(sender As System.Object, e As System.EventArgs) Handles btnLookupProvider.Click
		Dim number As String = Regex.Replace(TypedPublisherInformationBindingSource.MobileTelephone, "[^0-9]", "")

		Dim areaCode As Integer
		Dim prefix As Integer
		Dim fourDigits As Integer

		Dim url As String = "http://www.fonefinder.net/findome.php?npa={0}&nxx={1}&thoublock={2}&usaquerytype=Search+by+Number"

		If number.Length >= 10 Then

			fourDigits = number.Substring(number.Length - 4, 4)
			prefix = number.Substring(number.Length - 7, 3)
			areaCode = number.Substring(number.Length - 10, 3)

			Process.Start(String.Format(url, areaCode, prefix, fourDigits))

		End If

	End Sub
End Class